package com.obtk.property.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.util.StringUtil;
import com.obtk.property.dao.RepairMapper;
import com.obtk.property.pojo.Repair;
import com.obtk.property.service.RepairService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Map;

@Service
public class RepairServiceImpl implements RepairService {

    @Autowired
    private RepairMapper repairMapper;

    @Override
    public List<Repair> list() {
        return repairMapper.list();
    }

    @Override
    public List<Repair> findAll() {
        return repairMapper.selectAll();
    }

    @Override
    public Page<Repair> search(Map searchMap) {
        Example example = new Example(Repair.class);
        int pageNum =1;
        int pageSize=2;
        if(searchMap !=null) {
            Example.Criteria criteria = example.createCriteria();
            if (StringUtil.isNotEmpty((String) searchMap.get("startTime"))) {
                criteria.andGreaterThanOrEqualTo("createTime", searchMap.get("startTime"));
            }
            if (StringUtil.isNotEmpty((String) searchMap.get("endTime"))) {
                criteria.andGreaterThanOrEqualTo("createTime", searchMap.get("endTime"));
            }
            if (StringUtil.isNotEmpty((String) searchMap.get("deviceName"))) {
                criteria.andLike("deviceName", "%" + (String) searchMap.get("deviceName") + "%");
            }
            //分页

            if((Integer) searchMap.get("pageNum") !=null){
                pageNum = (Integer) searchMap.get("pageNum");
            }
            if((Integer) searchMap.get("pageSize") !=null){
                pageSize = (Integer) searchMap.get("pageSize");
            }
        }
        PageHelper.startPage(pageNum,pageSize);//使用PageHelper插件完成分页
        Page<Repair> Repair = (Page<Repair>) repairMapper.selectByExample(example);
        return Repair;
    }


    @Override
    public Boolean add(Repair repair) {
        int row = repairMapper.insert(repair);
        if (row>0){
            return true;
        }
        return false;
    }

    @Override
    public Repair findById(Integer repairId) {
        return repairMapper.selectByPrimaryKey(repairId);
    }

    @Override
    public Boolean update(Repair repair) {
        int row = repairMapper.updateByPrimaryKeySelective(repair);
        if(row>0){
            return true;
        }
        return false;
    }

    @Override
    public Boolean del(List<Integer> ids) {
       for (Integer id:ids){
           repairMapper.deleteByPrimaryKey(id);
       }
       return true;
    }
}
